home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / sockpacket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.4 KB  |  218 lines

  1. /*
  2.  *  $Id: sockpacket.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  sockpacket.c - linux sockpacket routines
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Copyright (c) 1996, 1997
  12.  *    The Regents of the University of California.  All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that: (1) source code distributions
  16.  * retain the above copyright notice and this paragraph in its entirety, (2)
  17.  * distributions including binary code include the above copyright notice and
  18.  * this paragraph in its entirety in the documentation or other materials
  19.  * provided with the distribution, and (3) all advertising materials mentioning
  20.  * features or use of this software display the following acknowledgement:
  21.  * ``This product includes software developed by the University of California,
  22.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  23.  * the University nor the names of its contributors may be used to endorse
  24.  * or promote products derived from this software without specific prior
  25.  * written permission.
  26.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  27.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  28.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  29.  */
  30.  
  31. #if (HAVE_CONFIG_H)
  32. #include "../include/config.h"
  33. #endif
  34. #include <sys/time.h>
  35.  
  36. #include <net/if.h>
  37. #if (__GLIBC__)
  38. #include <netinet/if_ether.h>
  39. #include <net/if_arp.h>
  40. #else
  41. #include <linux/if_arp.h>
  42. #include <linux/if_ether.h>
  43. #endif
  44.  
  45. #if (HAVE_PF_PACKET)
  46. #include <linux/if_packet.h>
  47. #ifndef SOL_PACKET
  48. #define SOL_PACKET 263
  49. #endif  /* SOL_PACKET */
  50. #endif  /* HAVE_PF_PACKET */
  51.  
  52. #include "../include/bpf.h"
  53. #include "../include/libnet.h"
  54.  
  55. #include "../include/gnuc.h"
  56. #ifdef HAVE_OS_PROTO_H
  57. #include "../include/os-proto.h"
  58. #endif
  59.  
  60. struct link_int *
  61. open_link_interface(char *device, char *ebuf)
  62. {
  63.     register struct link_int *l;
  64.     struct ifreq ifr;
  65. #if (HAVE_PF_PACKET)
  66.     struct packet_mreq mr;
  67.     struct sockaddr_ll sa;
  68. #endif
  69.  
  70.     l = (struct link_int *)malloc(sizeof (*l));
  71.     if (l == NULL)
  72.     {
  73.         sprintf(ebuf, "malloc: %s", ll_strerror(errno));
  74.         return (NULL);
  75.     }
  76.     memset(l, 0, sizeof (*l));
  77.  
  78. #if (HAVE_PF_PACKET)
  79.     l->fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  80. #else
  81.     l->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
  82. #endif
  83.     if (l->fd == -1)
  84.     {
  85.         sprintf(ebuf, "socket: %s", ll_strerror(errno));
  86.         goto bad;
  87.     }
  88.  
  89. #if (HAVE_PF_PACKET)
  90.     strcpy(ifr.ifr_name, device);
  91.     if (ioctl(l->fd, SIOCGIFINDEX, &ifr) < 0)
  92.     {
  93.         sprintf(ebuf, "ioctl: SIOCGIFINDEX: %s", ll_strerror(errno));
  94.         goto bad;
  95.     }
  96.     memset(&sa, 0, sizeof(sa));
  97.     sa.sll_family = AF_PACKET;
  98.     sa.sll_ifindex = ifr.ifr_ifindex;
  99.     sa.sll_protocol = htons(ETH_P_ALL);
  100.  
  101.     memset(&mr, 0, sizeof (mr));
  102.     mr.mr_ifindex = ifr.ifr_ifindex;
  103.     mr.mr_type = PACKET_MR_ALLMULTI;
  104.  
  105.     if (setsockopt(l->fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (char *)&mr,
  106.         sizeof (mr)) < 0)
  107.     {
  108.         sprintf(ebuf, "setsockopt %s: %s", device, ll_strerror(errno));
  109.         goto bad;
  110.     }
  111. #endif
  112.  
  113.     memset(&ifr, 0, sizeof (ifr));
  114.     strncpy(ifr.ifr_name, device, sizeof (ifr.ifr_name));
  115.     if (ioctl(l->fd, SIOCGIFHWADDR, &ifr) < 0 )
  116.     {
  117.         sprintf(ebuf, "SIOCGIFHWADDR: %s", ll_strerror(errno));
  118.         goto bad;
  119.     }
  120.  
  121.     switch (ifr.ifr_hwaddr.sa_family)
  122.     {
  123.         case ARPHRD_ETHER:
  124.         case ARPHRD_METRICOM:
  125.             l->linktype = DLT_EN10MB;
  126.             l->linkoffset = 0xe;
  127.             break;
  128.         case ARPHRD_SLIP:
  129.         case ARPHRD_CSLIP:
  130.         case ARPHRD_SLIP6:
  131.         case ARPHRD_CSLIP6:
  132.         case ARPHRD_PPP:
  133.             l->linktype = DLT_RAW;
  134.             break;
  135.         default:
  136.             sprintf(ebuf, "unknown physical layer type 0x%x",
  137.                 ifr.ifr_hwaddr.sa_family);
  138.         goto bad;
  139.     }
  140.     return (l);
  141.  
  142. bad:
  143.     if (l->fd >= 0)
  144.     {
  145.         close(l->fd);
  146.     }
  147.     free(l);
  148.     return (NULL);
  149. }
  150.  
  151.  
  152. int
  153. close_link_interface(struct link_int *l)
  154. {
  155.     return (close(l->fd));
  156. }
  157.  
  158.  
  159. int
  160. write_link_layer(struct link_int *l, const u_char *device, u_char *buf, int len)
  161. {
  162.     int c;
  163.     struct sockaddr sa;
  164.  
  165.     memset(&sa, 0, sizeof (sa));
  166.     strncpy(sa.sa_data, device, sizeof (sa.sa_data));
  167.  
  168.     c = sendto(l->fd, buf, len, 0, &sa, sizeof (sa));
  169.     if (c != len)
  170.     {
  171. #if (__DEBUG)
  172.         fprintf(stderr, "write_link_layer: %d bytes written (%s)\n", c,
  173.             strerror(errno));
  174. #endif
  175.     }
  176.     return (c);
  177. }
  178.  
  179.  
  180. struct ether_addr *
  181. get_hwaddr(struct link_int *l, const u_char *device, char *ebuf)
  182. {
  183.     int fd;
  184.     struct ifreq ifr;
  185.     struct ether_addr *eap;
  186.     /*
  187.      *  XXX - non-re-entrant!
  188.      */
  189.     static struct ether_addr ea;
  190.  
  191.     /*
  192.      *  Create dummy socket to perform an ioctl upon.
  193.      */
  194.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  195.     if (fd < 0)
  196.     {
  197.         sprintf(ebuf, "get_hwaddr: %s", strerror(errno));
  198.         return (NULL);
  199.     }
  200.  
  201.     memset(&ifr, 0, sizeof(ifr));
  202.     eap = &ea;
  203.     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  204.  
  205.     if (ioctl(fd, SIOCGIFHWADDR, (char *)&ifr) < 0)
  206.     {
  207.         close(fd);
  208.         sprintf(ebuf, "get_hwaddr: %s", strerror(errno));
  209.         return (NULL);
  210.     }
  211.     memcpy(eap, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
  212.     close(fd);
  213.     return (eap);
  214. }
  215.  
  216.  
  217. /* EOF */
  218.